package top.saplf.sileny.util

import java.security.MessageDigest

/**
 * @author Saplf
 */
private val md5Crypt: MessageDigest by lazy { MessageDigest.getInstance("MD5") }

fun byte2Hex(byte: Byte): String {
    val result = Integer.toHexString(byte.toInt() and 0xFF)
    return if (result.length == 1) "0$result" else result
}

/**
 * md5 编码
 */
fun String.md5Lower32(): String {
    md5Crypt.update(toByteArray())
    return md5Crypt.digest().map(::byte2Hex).joinToString("")
}
fun String.md5Upper32(): String = md5Lower32().toUpperCase()

fun ByteArray.md5Lower32(): ByteArray {
    md5Crypt.update(this)
    return md5Crypt.digest()
}
